package tt.dz.common;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.Set;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class SMSMessageUtil
{
  private static String SMSMESSAGEURL;
  private static String CDKEY;
  private static String PASSWORD;
  private static boolean sendFlag;
  
  static
  {
    Properties prop = new Properties();
    
    InputStream in = null;
    try
    {
      in = SMSMessageUtil.class.getClassLoader().getResourceAsStream("ms-context.properties");
      prop.load(in);
      
      SMSMESSAGEURL = prop.getProperty("SMSMessage.url").trim();
      CDKEY = DzDigester.decryptCommonInfo(prop.getProperty("SMSMessage.cdKey"));
      PASSWORD = DzDigester.decryptCommonInfo(prop.getProperty("SMSMessage.password"));
      sendFlag = Boolean.valueOf(prop.getProperty("sms.send.flag").trim()).booleanValue(); 
    }
    catch (Exception e)
    {
      LogUtil.error("加载短信发送资源信息异常", e);
      sendFlag = true;
    }
    finally
    {
      if (in != null) {
        try
        {
          in.close();
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
      }
    }
  }
  
  public static String sendMessage(String phone, String message)
    throws Exception
  {
    if (!sendFlag) {
      return "";
    }
    Map<String, String> map = new HashMap();
    map.put("cdkey", CDKEY);
    map.put("password", PASSWORD);
    map.put("phone", phone);
    map.put("message", message);
    String xml = post(SMSMESSAGEURL, map);
    return xml;
  }
  
  public static String getRandomCode()
  {
    return getRandomCode(6);
  }
  
  public static String getRandomCode(int num)
  {
    int[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    Random rand = new Random();
    for (int i = 10; i > 1; i--)
    {
      int index = rand.nextInt(i);
      int tmp = array[index];
      array[index] = array[(i - 1)];
      array[(i - 1)] = tmp;
    }
    StringBuffer randomCode = new StringBuffer();
    for (int i = 0; i < num; i++) {
      randomCode.append(array[i]);
    }
    return randomCode.toString();
  }
  
  public static String post(String url, Map<String, String> params)
    throws Exception
  {
    HttpClient httpclient = HttpClients.createDefault();
    String body = null;
    HttpPost httpost = new HttpPost(url);
    List<NameValuePair> nvps = new ArrayList();
    Set<String> keySet = params.keySet();
    for (String key : keySet) {
      nvps.add(new BasicNameValuePair(key, (String)params.get(key)));
    }
    try
    {
      httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
      HttpResponse response = httpclient.execute(httpost);
      HttpEntity entity = response.getEntity();
      body = EntityUtils.toString(entity);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      LogUtil.error("请求短信接口发生异常", e);
      throw e;
    }
    return body;
  }
}
